package com.wwwarehouse.contract.orders.place_order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.adapter.ShoppingCartAdapter;
import com.wwwarehouse.contract.bean.ConfirmOrderParams;
import com.wwwarehouse.contract.bean.OrderDetailParams;
import com.wwwarehouse.contract.bean.ShoppingCartBean;
import com.wwwarehouse.contract.bean.ShoppingCartListBean;
import com.wwwarehouse.contract.contract.TheParentFragment;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.program_operation.MyListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import contract.wwwarehouse.com.contract.R;

/**
 * Created by feng.gao on 2017/10/18.
 */

public class ShoppingCartFragment extends TheParentFragment {
	private MyListView myListView;
	private List<ShoppingCartListBean.ListBean> list = new ArrayList<>();
	private ShoppingCartAdapter mShoppingAdapter;
	private String mDemanId;
	private ConfirmOrderParams mConfirmParams;

	@Override
	protected int getContentId() {
		return R.layout.shopping_cart_listview_layout;
	}

	@Override
	protected void init(View view) {
		myListView = (MyListView) view.findViewById(R.id.shopping_cart_lv);
		myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				OrderDetailParams params = new OrderDetailParams();
				params.setDemandId(mDemanId);
				params.setSupplierBusinessId(list.get(position).getSupplierBusinessId());
				params.setSupplierName(list.get(position).getSupplierName());
				params.setLogo(list.get(position).getSupplierLogo());
				params.setTotalPrice(list.get(position).getTotalPrice());
				Bundle bundle = new Bundle();
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_ORDER_DETAILS_PARAMS , params);
				OrderDetailViewPagerFragment orderDetailViewPagerFragment = new OrderDetailViewPagerFragment();
				orderDetailViewPagerFragment.setArguments(bundle);
				pushFragment(orderDetailViewPagerFragment);
			}
		});
	}

	@Override
	public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
		if (enter) {
			if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA) != null) {
				final ShoppingCartListBean listBean = (ShoppingCartListBean) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA);
				if (StringUtils.isNoneNullString(listBean.getDemanId())) {
					mDemanId = listBean.getDemanId();
				}
				int position = getArguments().getInt(ContractConstant.KEY_BUNDLE_POSITION);
				int pageNum = getArguments().getInt(ContractConstant.KEY_BUNDLE_PAGENUM);
				list.clear();
				for (int i = position * pageNum; i < (position + 1) * pageNum; i++) {
					if (i >= 0 && i < listBean.getList().size()) {
						list.add(listBean.getList().get(i));
					}
				}
				myListView.post(new Runnable() {
					@Override
					public void run() {
						mShoppingAdapter = new ShoppingCartAdapter(list, mActivity,myListView.getHeight() - myListView.getPaddingBottom() - myListView.getPaddingTop());
						mShoppingAdapter.setPlaceOrderListener(new ShoppingCartAdapter.PlaceOrderListener() {
							@Override
							public void toPlaceOrder(int position) {
								//先请求接口是否成功，然后跳转到确认订单页面
								Map<String, Object> map = new HashMap<>();
								map.put("demandId", mDemanId);
								map.put("supplierBusinessId", list.get(position).getSupplierBusinessId());
								httpPost(ContractConstant.CONFIRM_ORDER_METHOD, map, 1, true, "正在加载...");
								mConfirmParams = new ConfirmOrderParams();
								mConfirmParams.setName(list.get(position).getSupplierName());
								mConfirmParams.setLogo(list.get(position).getSupplierLogo());
								mConfirmParams.setSupplierBusinessId(list.get(position).getSupplierBusinessId());
								mConfirmParams.setDemandId(mDemanId);
							}
						});
						myListView.setAdapter(mShoppingAdapter);
					}
				});

//
			}
		}
		return null;
	}

	@Override
	public void setTitle() {

	}

	@Override
	public void requestDatas() {

	}

	@Override
	public void onSuccess(CommonClass commonClass, int what) {
		if (what == 1) {
			if (commonClass.getCode().equals("0")) {
				//跳转到确认订单页面
				if (mConfirmParams != null) {
					Bundle bundle = new Bundle();
					bundle.putSerializable(ContractConstant.KEY_BUNDLE_CONFIRM_ORDER, mConfirmParams);
					ConfirmOrderFragment confirmOrderFragment = new ConfirmOrderFragment();
					confirmOrderFragment.setArguments(bundle);
					pushFragment(confirmOrderFragment);
				}

			} else {
				toast(commonClass.getMsg());
			}
		}

	}

	@Override
	public void onReLoad(int code) {

	}
}
